﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DunSilverlight.Web.Comm;
using DunLibrary.Dun;
using System.ServiceModel.Activation;
using System.Data;
using NHibernate;

namespace DunSilverlight.Web.DunWcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“CaseCatalogService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class CaseCatalogService :BaseService<CaseCatalog>, ICaseCatalogService
    {

        /// <summary>
        /// 批量结案/开案记录
        /// </summary>
        /// <param name="jsonIds"></param>
        /// <param name="check"></param>
        /// <returns></returns>
        public string CaseFinish(string jsonIds, bool check)
        {
            try
            {
                using (ISession sess = HbmSessionFactory.OpenSession())
                {
                    IDbConnection cnn = sess.Connection;

                    IList<int> ids = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<int>>(jsonIds);


                    string idstr = string.Join(",", ids);

                    string sql;
                    if (check)
                    {
                        sql = string.Format("update Dun_CaseCatalog set IsFinish ={0} where Id in ({1});", 1, idstr);
                        sql += string.Format("update Dun_CaseCatalog set FinishTime = getDate() where Id in ({0}) and FinishTime is null;", idstr);
                    }
                    else
                    {
                        sql = string.Format("update Dun_CaseCatalog set IsFinish ={0},FinishTime=null where Id in ({1});", 0, idstr);
                    }


                    //开案设置为null，结案设置为当前时间
                    //string finishTime = check ? "getDate()" : "null";

                    //string sql = string.Format("update Dun_CaseCatalog set IsFinish ={0},FinishTime={1} where Id in ({2});", check ? 1 : 0, finishTime,idstr);

                    //if (check)
                    //{
                    //    sql += "update Dun_CaseCatalog set IsFinish ={0},FinishTime={1} where Id in ({2});";
                    //}

                    IDbCommand cmd = cnn.CreateCommand();
                    cmd.CommandText = sql;
                    int result = cmd.ExecuteNonQuery();

                    return PackJsonResult("true", result.ToString(), string.Empty);
                }
            }
            catch (Exception ex)
            {
                return PackJsonResult("false", "0", ex.Message);
            }
        }
    }
}
